package com.ptp.backendcertms.business.controller;

import com.ptp.backendcertms.api.external.dto.req.FileUploadReqDto;
import com.ptp.backendcertms.api.external.dto.req.ImportResultReqDto;
import com.ptp.backendcertms.api.external.dto.res.ImportResultResDto;
import com.ptp.backendcertms.api.external.dto.res.ResponseDto;
import com.ptp.backendcertms.basiccompnents.http.ResponseWrapper;
import com.ptp.backendcertms.business.service.IFileBusinessService;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 文件业务控制器
 *
 * @author songcongcong
 * @date 2025/5/1
 */
@RestController
@RequestMapping("/cms")
public class FileBusinessController {
    @Resource
    private IFileBusinessService fileBusinessService;

    /**
     * 导入证书信息接口
     */
    @RequestMapping(value = "/file/importCertInfo", method = RequestMethod.POST)
    public ResponseDto<String> importCertInfo(@RequestParam("file") MultipartFile file, @Valid FileUploadReqDto reqDto, BindingResult bindingResult) {
        return ResponseWrapper.responseReturnWithValid(bindingResult, "[文件管理]-导入证书信息", () ->
                fileBusinessService.importCertInfo(file));
    }

    /**
     * 导入报告信息接口
     */
    @RequestMapping(value = "/file/importReportInfo", method = RequestMethod.POST)
    public ResponseDto<String> importReportInfo(@RequestParam("file") MultipartFile file, @Valid FileUploadReqDto reqDto, BindingResult bindingResult) {
        return ResponseWrapper.responseReturnWithValid(bindingResult, "[文件管理]-导入报告信息", () ->
                fileBusinessService.importReportInfo(file));
    }

    /**
     * 查询导入结果
     */
    @RequestMapping(value = "/file/getImportResult", method = RequestMethod.POST)
    public ResponseDto<ImportResultResDto> getImportResult (@RequestBody @Valid ImportResultReqDto reqDto, BindingResult bindingResult) {
        return ResponseWrapper.responseReturnWithValid(bindingResult, "[文件管理]-查询导入结果", () ->
                fileBusinessService.getImportResult(reqDto));
    }

    /**
     * 下载证书模版
     */
    @RequestMapping(value = "/file/downloadCertTemplate", method = RequestMethod.GET)
    public ResponseEntity<org.springframework.core.io.Resource> downloadCertTemplate() {
        return fileBusinessService.downloadCertTemplate();
    }

    /**
     * 下载报告模版
     */
    @RequestMapping(value = "/file/downloadReportTemplate", method = RequestMethod.GET)
    public ResponseEntity<org.springframework.core.io.Resource> downloadReportTemplate() {
        return fileBusinessService.downloadReportTemplate();
    }
}
